package IteratorPattern;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 迭代器模式
 * 书架
 * 具体的集合
 */
public class BookShelf implements Aggregate {
    private List<Book> books;//该书架的书

    public BookShelf() {
        this.books = new ArrayList<>();
    }

    public Book getBookAt(int index) {
        return books.get(index);
    }

    public void appendBook(Book book) {
        this.books.add(book);
    }

    public int getLength() {
        return books.size();
    }

    /**
     * 返回迭代器
     *
     * @return
     */
    @Override
    public Iterator iterator() {
        return new BookShelfIterator(this);
    }
}

//public class BookShelf implements Aggregate{
//    private Book[] books;
//    private int last=0;
//
//    public BookShelf(int maxsize) {
//        this.books = new Book[maxsize];
//    }
//
//    public Book getBookAt(int index) {
//        return books[index];
//    }
//
//    public void appendBook(Book book){
//        this.books[last]=book;
//        last++;
//    }
//
//    public int getLength() {
//        return last;
//    }
//
//    /**
//     * 返回迭代器
//     * @return
//     */
//    @Override
//    public Iterator iterator() {
//        return new BookShelfIterator(this);
//    }
//}